package com.ihrm.system.service.impl;


import com.ihrm.common.entity.ResultCode;
import com.ihrm.common.exception.CommonException;
import com.ihrm.common.utils.BeanMapUtils;
import com.ihrm.common.utils.IdWorker;
import com.ihrm.common.utils.PermissionConstants;
import com.ihrm.domain.system.*;
import com.ihrm.system.mapper.PermissionApiMapper;
import com.ihrm.system.mapper.PermissionMapper;
import com.ihrm.system.mapper.PermissionMenuMapper;
import com.ihrm.system.mapper.PermissionPointMapper;
import com.ihrm.system.service.api.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private PermissionMenuMapper permissionMenuMapper;
    @Autowired
    private PermissionPointMapper permissionPointMapper;
    @Autowired
    private PermissionApiMapper permissionApiMapper;
    @Autowired
    private IdWorker idWorker;

    @Override
    public void add(Map<String, Object> map) throws Exception {
        String id = idWorker.nextId() + "";
        //1.通过map构造权限对象
        Permission permission = BeanMapUtils.mapToBean(map, Permission.class);
        permission.setId(id);
        //2.保存
        permissionMapper.insertSelective(permission);
        //3.根据类型构造不同的资源对象(菜单，按钮，api)
        Integer type = permission.getType();
        switch (type) {
            case PermissionConstants.PY_MENU:
                PermissionMenu permissionMenu = BeanMapUtils.mapToBean(map, PermissionMenu.class);
                permissionMenu.setId(id);
                permissionMenuMapper.insertSelective(permissionMenu);
                break;
            case PermissionConstants.PY_POINT:
                PermissionPoint permissionPoint = BeanMapUtils.mapToBean(map, PermissionPoint.class);
                permissionPoint.setId(id);
                permissionPointMapper.insertSelective(permissionPoint);
                break;
            case PermissionConstants.PY_API:
                PermissionApi permissionApi = BeanMapUtils.mapToBean(map, PermissionApi.class);
                permissionApi.setId(id);
                permissionApiMapper.insertSelective(permissionApi);
                break;
            default:
                throw new CommonException(ResultCode.FAIL);
        }

    }

    @Override
    public void update(Map<String, Object> map) throws Exception {
        //1.通过map构造权限对象
        Permission permission = BeanMapUtils.mapToBean(map, Permission.class);
        permissionMapper.updateByPrimaryKeySelective(permission);
        //2.根据类型构造不同的资源对象(菜单，按钮，api)
        Integer type = permission.getType();
        switch (type) {
            case PermissionConstants.PY_MENU:
                PermissionMenu permissionMenu = BeanMapUtils.mapToBean(map, PermissionMenu.class);
                permissionMenuMapper.updateByPrimaryKeySelective(permissionMenu);
                break;
            case PermissionConstants.PY_POINT:
                PermissionPoint permissionPoint = BeanMapUtils.mapToBean(map, PermissionPoint.class);
                permissionPointMapper.updateByPrimaryKeySelective(permissionPoint);
                break;
            case PermissionConstants.PY_API:
                PermissionApi permissionApi = BeanMapUtils.mapToBean(map, PermissionApi.class);
                permissionApiMapper.updateByPrimaryKeySelective(permissionApi);
                break;
            default:
                throw new CommonException(ResultCode.FAIL);
        }
    }

    @Override
    public void deleteById(String id) {
        Permission permission = permissionMapper.selectByPrimaryKey(id);
        Integer type = permission.getType();
        if (type == PermissionConstants.PY_MENU) {
            permissionMenuMapper.deleteByPrimaryKey(id);
        } else if (type == PermissionConstants.PY_POINT) {
            permissionPointMapper.deleteByPrimaryKey(id);
        } else if (type == PermissionConstants.PY_API) {
            permissionApiMapper.deleteByPrimaryKey(id);
        } else {
            throw new CommonException(ResultCode.FAIL);
        }
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Map<String, Object> findById(String id) {
        Permission permission = permissionMapper.selectByPrimaryKey(id);
        Integer type = permission.getType();
        Object object = null;
        if (type == PermissionConstants.PY_MENU) {
            object = permissionMenuMapper.selectByPrimaryKey(id);
        } else if (type == PermissionConstants.PY_POINT) {
            object = permissionPointMapper.selectByPrimaryKey(id);
        } else if (type == PermissionConstants.PY_API) {
            object = permissionApiMapper.selectByPrimaryKey(id);
        } else {
            throw new CommonException(ResultCode.FAIL);
        }
        Map<String, Object> map = BeanMapUtils.beanToMap(object);
        map.put("name", permission.getName());
        map.put("type", permission.getType());
        map.put("code", permission.getCode());
        map.put("description", permission.getDescription());
        map.put("enVisible", permission.getEnVisible());
        return map;
    }

    @Override
    public List<Permission> findAll(Map<String, Object> map) {
        Integer type = (Integer) map.get("type");
        String pid = (String) map.get("pid");
        Integer enVisible = (Integer) map.get("enVisible");
        PermissionExample permissionExample = new PermissionExample();
        PermissionExample.Criteria criteria = permissionExample.createCriteria();
        if (enVisible != null) {
            criteria.andEnVisibleEqualTo(enVisible);
        }
        if (pid != null) {
            criteria.andPidEqualTo(pid);
        }
        if (type != null) {
            if (type == 0) {
                List<Byte> list = new ArrayList();
                list.add((byte) 1);
                list.add((byte) 2);
                list.add((byte) 3);
                criteria.andTypeIn(list);
            } else if (type == 1) {
                criteria.andTypeEqualTo((byte) 1);
            } else if (type == 2) {
                criteria.andTypeEqualTo((byte) 2);
            } else if (type == 3) {
                criteria.andTypeEqualTo((byte) 3);
            }
        }
        return permissionMapper.selectByExample(permissionExample);
    }
}
